Repository navigation
Conversation
When an aliased import inside a deferred block (or one whose child has deferred nodes) is left as a raw import tag for the next pass, the parent's variables and macros that the imported template reads were not reconstructed, so the second pass saw them as undefined. Collect the words the imported template uses and reconstruct/defer them in front of the deferred import tag, as other deferred tags do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…edImport Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The second pass used jinjava.newInterpreter(), whose config has the test base's legacy overrides, while the eager pass used its own. Build both from one config method that only differs in execution mode. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…erred import
When an aliased import of a macro-defining template is deferred, the parent
macros the imported template reads are reconstructed in front of it. A
parent macro that uses caller(), called by the imported template with
{% call %}, failed to reconstruct: caller() couldn't be resolved, which is
a FATAL error, and the rebuilt macro lost its {{ caller() }}.
Like EagerCallTag, register a deferred placeholder caller macro in a
temporary scope while reconstructing, so caller() stays in the macro.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AlpriElse
commented
Nov 24, 2026
Comment on lines
+783
to
+794
| public void itPreservesParentCallerMacroCalledByDeferredImportWithMacro() { | ||
| setupResourceLocator(); | ||
| String firstPassResult = interpreter.render( | ||
| "{% macro k(p) %}<{{ p }}:{{ caller() }}>{% endmacro %}{% if deferred %}" + | ||
| "{% import 'macro-and-set-calling-parent-caller-macro.jinja' as lib %}{{ lib.v }}" + | ||
| "{% endif %}" | ||
| ); | ||
| assertThat(interpreter.getErrors()).isEmpty(); | ||
| RenderResult secondPass = renderSecondPassInDefaultMode(firstPassResult); | ||
| assertThat(secondPass.getErrors()).isEmpty(); | ||
| assertThat(secondPass.getOutput()).isEqualTo("<0:x>"); | ||
| } |
Owner
Author
There was a problem hiding this comment.
initial fix caused a regression for this edge case
| Build | Eager pass | Second pass |
|---|---|---|
master |
no errors | FATAL Could not resolve function 'k' |
| With initial fix | FATAL Could not resolve function 'caller' |
no errors, but <0:> instead of <0:x> |
With the follow-up commit caller() fix (4c86aedc) |
no errors | <0:x> (correct) |
Honestly, haven't touched this codebase in a looong time, so I don't remember the semantics and consequences around messing with the scope stack
I have a feeling a better solution here would be to "detect" that a {% call %} is in the import and just deny list it from eager execution by throwing DeferredValueException 🤷
gonna surrender this one and close this pr out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Touches the same code as HubSpot#1369, but fixes a different problem.
Context
EagerImportTaghandles{% import 'lib' as x %}in eager execution mode.When the import is inside a deferred block, such as
{% if deferred %}, andlibdefines a macro, the import can't be inlined. It's written back out as a raw{% import %}tag, which runs again when the output is fully rendered later.Problem
When the import was left as a raw tag, the parent template's state that
libreads, such as earliersettags and macros defined in the parent, wasn't kept in the eager output.On the second render pass,
libsaw those as undefined.EagerImportTagthrows away the child's render in this case and callsAliasedEagerImportingStrategy.handleDeferredTemplateFile. That method records only the path expression and the alias. Nothing recorded which parent variables the child reads, so they were never written out in front of the import.includeandfrom ... importaren't affected, because they inline the child's render instead of running it again.Broken Examples
What this PR Does
When the import of a macro-defining template is deferred,
EagerImportTagnow collects the candidate names (variables and macros) that the imported template's source references. When it returns the raw import, it writes the parent's variables and macros among those names in front of it, usingEagerReconstructionUtils.hydrateReconstructionFromContextBeforeDeferringand aDeferredToken.EagerPrintTagandEagerForTagalready use this pattern. Eager output for the first example:Only
EagerImportTagchanges; the importing strategies and shared eager utilities don't. No existing test expectations change.Tests
EagerImportTagTest. All three fail onmasterand pass with this change:itPreservesParentVariableReadByDeferredImportWithMacro(fixturemacro-and-set-reading-parent-var.jinja): a parent variable.itPreservesParentMacroCalledByDeferredImportWithMacro(fixturemacro-and-set-calling-parent-macro.jinja): a parent macro.itPreservesParentCallerMacroCalledByDeferredImportWithMacro(fixturemacro-and-set-calling-parent-caller-macro.jinja): a parent macro that usescaller(), called with{% call %}. Rebuilding it registers a deferred placeholdercaller, asEagerCallTagdoes, so{{ caller() }}is kept.Eager*Test,NonRevertingEagerTest,*Import*Test,DeferredTest,*Call*Test,*Macro*Test) all pass (834 tests). The fullmvn verifypasses (2252 tests).Results
The same templates as the Broken Examples above:
16Could not resolve function 'm'[0]🤖 Generated with Claude Code